package com.ctsi.ssdc.service.impl;


import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallAddress;
import com.ctsi.ssdc.domain.LitemallAddressExample;
import com.ctsi.ssdc.service.LitemallAddressService;
import com.ctsi.ssdc.repository.LitemallAddressRepository;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallAddress.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallAddressServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallAddressRepository, LitemallAddress, Integer, LitemallAddressExample> 
	implements LitemallAddressService {

    @Autowired
    LitemallAddressRepository litemallAddressRepository;


    /**
    * GET  /litemallAddresss : get the litemallAddresss firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallAddress> findFirstStringColumn(String name,Pageable pageable){
        String str = name;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallAddressExample litemallAddressExample = new LitemallAddressExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallAddressExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallAddressExample.createCriteria();
        } else{
            litemallAddressExample.createCriteria().andNameLike("%" + str +"%");
        }
        List<LitemallAddress>  data = litemallAddressRepository.selectByExample(litemallAddressExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallAddressRepository.countByExample(litemallAddressExample);
        }
        return new PageResult<LitemallAddress>(data,count,count);
    }

    //wx 方法
    @Override
    public List<LitemallAddress> queryByUid(Integer userId) {

            LitemallAddressExample example = new LitemallAddressExample();
            example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
            return litemallAddressRepository.selectByExample(example);

    }

    @Override
    public LitemallAddress query(Integer userId, Integer id) {
        LitemallAddressExample example = new LitemallAddressExample();
        example.or().andIdEqualTo(id).andUserIdEqualTo(userId).andDeletedEqualTo(false);
        return litemallAddressRepository.selectOneByExample(example);
    }

    @Override
    public void resetDefault(Integer userId) {
        LitemallAddress address = new LitemallAddress();
        address.setIsDefault(false);
        address.setUpdateTime(ZonedDateTime.now());
        LitemallAddressExample example = new LitemallAddressExample();
        example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        litemallAddressRepository.updateByExampleSelective(address, example);
    }

    @Override
    public int add(LitemallAddress address) {
        address.setAddTime(ZonedDateTime.now());
        address.setUpdateTime(ZonedDateTime.now());
        return litemallAddressRepository.insertSelective(address);

    }

    @Override
    public LitemallAddress findDefault(Integer userId) {
        LitemallAddressExample example = new LitemallAddressExample();
        example.or().andUserIdEqualTo(userId).andIsDefaultEqualTo(true).andDeletedEqualTo(false);
        return litemallAddressRepository.selectOneByExample(example);
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
